<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <link rel="stylesheet" href="/component/pear/css/pear.css"/>
    <style>
        .pear-btn-md {
            height: 38px;
            line-height: 38px;
            padding: 0 10px;
            font-size: 13px;
        }
    </style>
</head>
<body class="pear-container">
<div class="layui-card">
    <div class="layui-card-body">
        <form class="layui-form" action="">
            <div class="layui-form-item">
                <div class="layui-form-item layui-inline">
                    <label class="layui-form-label">打印个数</label>
                    <div class="layui-input-inline">
                        <input type="number" name="numberOfPrints" placeholder="" class="layui-input"
                               lay-verify="required" lay-reqtext="请输入打印个数!">
                    </div>
                </div>
                <div class="layui-form-item layui-inline">
                    <button class="pear-btn pear-btn-md pear-btn-primary" lay-submit lay-filter="generateQrcode">
                        <i class="layui-icon layui-icon-search"></i>
                        生成标签
                    </button>
                    <button type="reset" class="pear-btn pear-btn-md">
                        <i class="layui-icon layui-icon-refresh"></i>
                        重置
                    </button>
                </div>
                <div class="layui-form-item layui-inline" style="position: absolute;right: 0px;">
                    <button class="pear-btn pear-btn-md pear-btn-primary" lay-submit lay-filter="printLabel"
                            style="border: 1px solid #3bc922 !important;background-color: #3bc922 !important;">
                        <i class="layui-icon layui-icon-search"></i>
                        打印标签
                    </button>
                </div>
            </div>
        </form>
    </div>
</div>
<div class="layui-card uuidTable" style="display: none">
    <div class="layui-card-body">
        <table id="electronArchives-table" lay-filter="electronArchives-table"></table>
    </div>
</div>
<script src="/component/layui/layui.js"></script>
<script src="/component/pear/pear.js"></script>
<script th:inline="javascript">
    let buildStatus = false;
    let uuidList = null;
    let qrCodeId = null;

    layui.use(['table', 'form', 'jquery'], function () {
        let table = layui.table;
        let form = layui.form;
        let $ = layui.jquery;

        let cols = [
            [
                {title: '序号', field: 'serialNumber', align: 'center', width: 100},
                {title: '识别码', field: 'uuid', align: 'center'},
                {title: '生成时间', field: 'create_time', align: 'center'},
                {
                    title: '操作', align: 'center', minWidth: 250, templet: function (d) {
                        return ' <button plain class="pear-btn pear-btn-primary pear-btn-xs" lay-event="copy">复制识别码</button>';
                    }
                }
            ]
        ]

        form.on('submit(generateQrcode)', function (data) {
            $.ajax({
                url: '/archives/qrCode/generate',
                data: {"numberOfPrints": data.field.numberOfPrints},
                dataType: 'json',
                contentType: 'application/json',
                type: 'get',
                success: function (result) {
                    if (result.code === 200) {
                        console.log(result);
                        $(".uuidTable").css("display", "")
                        table.render({
                            elem: '#electronArchives-table',
                            url: '',
                            page: false,
                            cols: cols,
                            skin: 'line',
                            defaultToolbar: [{
                                title: '刷新',
                                layEvent: 'refresh',
                                icon: 'layui-icon-refresh',
                            }, 'filter', 'print', 'exports'],
                            id: 'electronArchives',
                            data: result.data.uuidList,
                            done: function (res, curr, count) {
                                if (count > 0) {
                                    buildStatus = true;
                                } else {
                                    buildStatus = false;
                                }
                                uuidList = result.data.uuidList;
                                qrCodeId = result.data.qrCodeId;
                            }
                        });
                    } else {
                        layer.msg(result.message, {icon: 2, time: 2000});
                    }
                }
            })
            return false;
        });

        table.on('tool(electronArchives-table)', function (obj) {
            var data = obj.data; // 获得当前行数据
            if (obj.event === 'copy') {
                copyToClipboard(data.uuid);
            }

        });

        function copyToClipboard(text) {
            // 创建一个临时文本区域
            const textarea = document.createElement('textarea');
            textarea.value = text;

            // 设置样式使其不可见但能被选中
            textarea.style.position = 'absolute';
            textarea.style.left = '-9999px';

            // 添加到页面并选中内容
            document.body.appendChild(textarea);
            textarea.select();

            try {
                // 执行复制命令
                const successful = document.execCommand('copy');
                const message = successful ? '复制成功' : '复制失败';
                layer.msg(message);
                return successful;
            } catch (err) {
                layer.msg('复制操作出错');
                return false;
            } finally {
                // 清理临时元素
                document.body.removeChild(textarea);
            }
        }

        form.on('submit(printLabel)', function (data) {
            if (qrCodeId == null) {
                layer.msg("请先生成标签", {icon: 2, time: 2000});
                return false;
            }
            parent.layer.open({
                title: '二维码生成',
                type: 2,
                // area: ['400px', '300px'],
                area: ['1px', '1px'], // 极小化弹窗
                content: '/archives/batch/qrCode/print?qrCodeId='+qrCodeId,
                shade: 0.5,
                kin: 'layui-layer-radius', // 自定义一个CSS类名来设置边框圆角
            });
            return false;
        });
    })
</script>
</body>
</html>